package com.ftg.learn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.entity.sys.User;
import com.ftg.learn.service.Iuser;
import com.ftg.learn.util.MD5;
import com.ftg.learn.util.ResultJson;
import com.ftg.learn.util.ResultJsonEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.security.MessageDigest;
import java.util.List;
import java.util.Objects;

/**
 * @author kangjx
 * @date 2022/10/21
 */
@Api(tags = "用户维护")
@RestController
@RequestMapping("/sys/user")
public class UserController {

    @Resource
    Iuser iuser;

    @GetMapping("/page")
    @ApiOperation("分页功能")
    public ResultJson page(
            @RequestParam(value = "page",required = false) @ApiParam(value = "页码",defaultValue = "1")int page,
            @RequestParam(value = "limit",required = false) @ApiParam(value = "显示条数",defaultValue = "10")int limit
            ,@RequestParam(value = "account",required = false) @ApiParam(value = "帐号",required = false) String account
    ){
        QueryWrapper<User> query = new QueryWrapper<>();
        if(Objects.nonNull(account)){
            query.like("u_account",account);
        }
        IPage<User> iPage = new Page<>(page,limit);
        return ResultJson.OK(iuser.page(iPage,query));
    }

    @GetMapping("/showallWithParam")
    @ApiOperation("带参")
    public ResultJson showallWithParam(@RequestParam(value = "account",required = false) @ApiParam(value = "帐号",required = false) String account){
        QueryWrapper<User> query = new QueryWrapper<>();
        if(Objects.nonNull(account)){
            query.like("u_account",account);
        }

        List<User> list = iuser.list(query);
        return ResultJson.OK(list);
    }


    @GetMapping("/showall")
    @ApiOperation("列出所有用户 信息")
    public ResultJson showAll(){
        return ResultJson.OK(iuser.list());
    }

    @PostMapping("/save")
    @ApiOperation("添加用户信息")
    public ResultJson saveUser(@ApiParam(value="实体")@RequestBody User user){
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("u_account",user.getUaccount());
        User temp = iuser.getOne(query);
        if(Objects.isNull(temp)){
            //对密码进行加密处理 [des rsa] md5
            user.setUpassword(MD5.encrypt2ToMD5(user.getUpassword()));
            boolean ist = iuser.save(user);
            return ist ? ResultJson.OK(ist) : ResultJson.FAILURE();
        }
        return  ResultJson.builder(ResultJsonEnum.USERACCOUTEXIST);
    }


    @GetMapping("/find/{id}/test/abc/def")
    @ApiOperation("按照 id查询一个")
    public ResultJson findUser(@ApiParam(value="编号") @PathVariable("id") Integer id){
        return ResultJson.OK(iuser.getById(id));
    }


    @PutMapping("/modify")
    @ApiOperation("修改")
    public ResultJson modifyUser(@ApiParam(value="实体")@RequestBody User user){
        return ResultJson.OK(iuser.updateById(user));
    }

    @DeleteMapping("/deleteById/{id}")
    @ApiOperation("修改")
    public ResultJson delteByid(@PathVariable("id")@ApiParam(value="主键") Serializable id){
        return ResultJson.OK(iuser.removeById(id));
    }

    @PostMapping("/roleForUser/{uid}")
    @ApiOperation("为用户分配角色")
    public ResultJson roleForUser(@PathVariable("uid")@ApiParam(value="用户编号") Integer uid
    ,@RequestParam("rids")@ApiParam(value="角色")List<Integer> rids){
        return iuser.giveRoleForUser(uid,rids);
    }




}
